﻿using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using TXTD.Admin.Data.Entitys.System;
using TXTD.Admin.Framework.ExpressionCore;
using TXTD.Admin.Interface.System;
using TXTD.Admin.Model.Modules.System;
using TXTD.Admin.Model.Paging;

namespace TXTD.Admin.Service.System
{
    public class SysRoleService : BaseService<SysRole>, ISysRoleService
    {
        public SysRoleService(DbContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
        {
        }
        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<SysRole> GetAllList()
        {
            return QueryAllData().ToList();
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <returns></returns>
        public PageResult<SysRole> GetPageList(SysRole model)
        {
            #region 动态凭借查询条件
            ExpressionHelper<SysRole> expressionHelper = new ExpressionHelper<SysRole>();
            expressionHelper.Contains(nameof(SysRole.RoleCode), model.RoleCode);
            expressionHelper.Contains(nameof(SysRole.RoleCode), model.RoleCode);
            var where = expressionHelper.GetExpression();
            #endregion
            return QueryPageList<SysRole>(where);
        }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int Add(SysRole model)
        {
            return InsertModel(model);
        }
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int Update(SysRole model)
        {
            return UpdateModel(model, model.RoleId);
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>

        public int DeleteById(string id)
        {
            return RemoveById(id);
        }
        /// <summary>
        /// 修改启用状态
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int UpdateEnabled(SysRole model)
        {
            return Update(new SysRole()
            {
                RoleId = model.RoleId,
                Enabled = model.Enabled
            });
        }
        /// <summary>
        /// 根据角色id获取角色菜单
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<string> GetRoleMenuByRoleId(string roleId)
        {
            List<string> result = db.Set<SysRoleMenu>().Where(t => t.RoleId == roleId).Select(t => t.MenuId).ToList();
            return result;
        }

        /// <summary>
        /// 保存角色菜单
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="menuIds"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int SaveRoleMenu(RoleMenuModel roleMenuModel)
        {
            //先删除角色的菜单
            db.Set<SysRoleMenu>().RemoveRange(db.Set<SysRoleMenu>().Where(t => t.RoleId == roleMenuModel.RoleId));
            //再保存角色的菜单
            if (roleMenuModel.MenuIds != null)
            {
                foreach (var menuId in roleMenuModel.MenuIds)
                {
                    db.Set<SysRoleMenu>().Add(new SysRoleMenu()
                    {
                        RoleId = roleMenuModel.RoleId,
                        MenuId = menuId
                    });
                }
            }
            return db.SaveChanges() + 1;
        }


    }
}
